/*
 * Copyright (C) 2010 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include <cutils/memory.h>

#include <utils/Log.h>

//#include <system/window.h>

#include <binder/IPCThreadState.h>
#include <binder/ProcessState.h>
#include <binder/IServiceManager.h>

#include <gui/Surface.h>
#include <gui/SurfaceComposerClient.h>

using namespace android;

#define CHECK_RET() do{ if(ret) printf("error:\t\tline:%d ret:%d\n", __LINE__, ret); } while(0)

int main(int argc, char** argv)
{
    printf("entry main\n");
    int ret=0;

	int width = 1600, height = 900, size= width*height*4;

    // create a client to surfaceflinger
    sp<SurfaceComposerClient> client = new SurfaceComposerClient();
    
    sp<SurfaceControl> surfaceControl = client->createSurface(
            String8("surface"), width, height, PIXEL_FORMAT_RGBA_8888, 0);
    SurfaceComposerClient::openGlobalTransaction();
    surfaceControl->setLayer(2900000);
	surfaceControl->setAlpha(1.0);
	surfaceControl->show();
    SurfaceComposerClient::closeGlobalTransaction();
	
	sp<GraphicBuffer> buffer = new GraphicBuffer(width, height, HAL_PIXEL_FORMAT_RGBA_8888,
            GRALLOC_USAGE_SW_WRITE_OFTEN | GRALLOC_USAGE_HW_FB
            );

    status_t err = buffer->initCheck();
    if (err != NO_ERROR) {
        printf("%s\n", strerror(-err));
        return 0;
    }


	while(1){
		void* vaddr;
		buffer->lock(
			GRALLOC_USAGE_SW_READ_OFTEN | GRALLOC_USAGE_SW_WRITE_OFTEN,
			&vaddr);

		memset(vaddr, 128, size);
		buffer->unlock();
	}


    printf("test complete. CTRL+C to finish.\n");

	sleep(5);
    return 0;
}
